<template>
  <div class="userContainer">
    <!--    用户管理表格区域-->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>用户管理</span>
      </div>
      <el-table
        :cell-style="{textAlign:'center'}"
        :data="tableData"
        :header-cell-style="{textAlign:'center'}"
        style="width: 100%"
      >
        <el-table-column
          label="昵称"
          prop="nickName"
        />
        <el-table-column
          label="用户名"
          prop="username"
        />
        <el-table-column
          label="真实姓名"
          prop="realName"
        />
        <el-table-column
          label="手机号"
          prop="mobile"
        />
        <el-table-column
          label="详细 地址"
          prop="address"
        />
        <el-table-column
          label="性别"
          prop="sex"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.sex === 1 ? '男' : "女" }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          :render-header="renderHeader"
        >
          <template slot-scope="scope">
            <el-button size="mini" type="danger" @click="delUserList(scope.row)">删除</el-button>
            <el-button size="mini" type="primary" @click="editUserList(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        style="margin: 20px 0;float: right"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getUserList"
      />
    </el-card>
    <!--    添加用户对话框-->
    <el-dialog
      title="添加用户"
      :visible.sync="addDialogVisible"
      width="30%"
    >
      <el-form ref="addUserForm" :model="addUserForm" :rules="rules" label-width="100px">
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="addUserForm.nickName"/>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addUserForm.username"/>
        </el-form-item>
        <el-form-item label="登录密码" prop="password">
          <el-input v-model="addUserForm.password" show-password />
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="addUserForm.realName"/>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="addUserForm.mobile"/>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="addUserForm.address"/>
        </el-form-item>
        <el-form-item label="性别" prop="sex" >
          <el-radio-group v-model="addUserForm.sex">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="0">女</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser('addUserForm')">确 定</el-button>
      </span>
    </el-dialog>
    <!--    编辑用户对话框-->
    <el-dialog
      title="编辑用户"
      :visible.sync="editDialogVisible"
      width="30%"
    >
      <el-form ref="editForm" :model="editForm" :rules="rules" label-width="100px">
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="editForm.nickName"/>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="editForm.username"/>
        </el-form-item>
        <el-form-item label="登录密码" prop="password">
          <el-input v-model="editForm.password" show-password />
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="editForm.realName"/>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="editForm.mobile"/>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="editForm.address"/>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="editForm.sex">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="0">女</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitUserData('editForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {addUser, delUser, editUser, getUser, queryUserList} from '@/api/user'

export default {
  name: 'User',
  data() {
    return {
      // 编辑用户表单数据
      editForm: {
        "id": 0,
        "mobile": "",
        "nickName": "",
        "password": "",
        "realName": "",
        "address": "",
        "sex": 0,
        "username": ""
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 添加用户表单数据
      addUserForm: {
        "mobile": "",
        "nickName": "",
        "password": "",
        "realName": "",
        "address": "",
        "sex": 0,
        "username": ""
      },
      // 对话框的显示与隐藏
      addDialogVisible: false,
      editDialogVisible: false,
      // 表格数据
      tableData: [],
      total: 0,
      id: '',
      // 数据校验
      rules: {
        nickName: [
          {required: true, message: '昵称不能为空'}
        ],
        username: [
          {required: true, message: '用户名不能为空'}
        ],
        sex: [
          {required: true, message: '请选择性别'}
        ],
        password: [
          {required: true, message: '请输入密码'}
        ]
      }
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    // 获取用户管理列表
    getUserList() {
      this.loading = true
      getUser().then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
        this.loading = false
      })
    },
    // 编辑用户操作
    editUserList(row) {
      const id = row.id
      this.id = id
      //  打开编辑对话框
      this.editDialogVisible = true
      // 通过ID获取用户信息
      queryUserList(id).then(res => {
        console.log(res)
        // 渲染编辑表单数据
        this.editForm = res.data
      })
    },
    // 提交编辑用户操作

    submitUserData(formName) {
      this.$refs[formName].validate((valid) => {
        console.info("edit",this.editForm)
        if (valid) {
          editUser(this.editForm).then(() => {
            this.$message.success('更新成功')
            // 重置表单
            this.$refs[formName].resetFields();
            //  关闭对话框
            this.editDialogVisible = false
            //  调用获取数据列表方法，更新列表数据
            this.getUserList()
          })
        } else {
          return false
        }
      })
      //  调用编辑用户接口

    },
    // 删除用户操作
    delUserList(row) {
      console.log(row.id)
      const id = row.id
      //  删除提示框
      this.$confirm('是否删除数据', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delUser(id)
      }).then(() => {
        this.getUserList()
        this.$message.success('删除成功')
      }).catch(function () {

      })
    },
    renderHeader(h) {
      return (
        <div>操作
          <el-button type='success' size='mini' onClick={() => this.handleAddItem()}
                     style='margin-left:20px'>添加</el-button>
        </div>
      )
    },
    // 打开添加用户对话框
    handleAddItem() {
      this.addDialogVisible = true
    },
    //  添加用户操作
    addUser(formName) {
      console.info("addUser",this.addUserForm)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addUser(this.addUserForm).then(() => {
            this.addDialogVisible = false
            // 重置表单
            this.$refs[formName].resetFields();
            this.$message.success('添加成功')
            this.getUserList()
          }).catch(err => {
            console.log(err)
          })
        } else {
          return false
        }
      })
    }
  }
}

</script>

<style lang="scss" scoped>
.userContainer {
  padding: 20px;
}

</style>
